package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.ApplicationDto;
import xyz.zhouzhaodong.crm.entity.Application;
import xyz.zhouzhaodong.crm.mapper.ApplicationMapper;
import xyz.zhouzhaodong.crm.mapper.CustomerMapper;
import xyz.zhouzhaodong.crm.service.IApplicationService;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 申请表（学校和专业） 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-18
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements IApplicationService {

    @Resource
    ApplicationMapper applicationMapper;

    @Resource
    CustomerMapper customerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addApplication(ApplicationDto applicationDto) {
        try {
            Application application = applicationDtoToApplication(applicationDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            application.setId(id);
            application.setCreateUser(applicationDto.getStaffId());
            application.setCreateTime(new Date());
            applicationMapper.insert(application);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteApplication(ApplicationDto applicationDto) {
        try {
            Application application = new Application();
            application.setId(applicationDto.getId());
            application.setFlag(1);
            application.setUpdateUser(applicationDto.getStaffId());
            application.setUpdateTime(new Date());
            applicationMapper.updateById(application);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateApplication(ApplicationDto applicationDto) {
        try {
            Application application = applicationDtoToApplication(applicationDto);
            application.setUpdateUser(applicationDto.getStaffId());
            application.setUpdateTime(new Date());
            applicationMapper.updateById(application);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectApplication(ApplicationDto applicationDto) {
        QueryWrapper<Application> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", applicationDto.getCustomerId());
        queryWrapper.eq("type", applicationDto.getType());
        if (applicationDto.getFlag() != null) {
            queryWrapper.eq("flag", applicationDto.getFlag());
        } else {
            queryWrapper.eq("flag", 0);
        }
        List<Application> list = applicationMapper.selectList(queryWrapper);
        return RestMessage.success(list);
    }

    @Override
    public RestMessage selectApplicationList(ApplicationDto applicationDto) {
        // 查询申请列表
        Page<ApplicationDto> page = new Page<>(applicationDto.getPageNo(),
                applicationDto.getPageSize() == 0 ? 10 : applicationDto.getPageSize());
        applicationDto.setFlag(applicationDto.getFlag() == null ? 0 : applicationDto.getFlag());
        IPage<ApplicationDto> list = applicationMapper.selectApplicationList(page, applicationDto);
        for(ApplicationDto a : list.getRecords()){
            String customerId = a.getCustomerId();
            a.setCustomer(customerMapper.selectById(customerId));
        }
        return RestMessage.success(list);
    }

    /**
     * 实体类转化
     *
     * @param applicationDto
     * @return
     */
    private Application applicationDtoToApplication(ApplicationDto applicationDto) {
        Application application = new Application();
        application.setId(applicationDto.getId());
        application.setCustomerId(applicationDto.getCustomerId());
        application.setApplyMajor(applicationDto.getApplyMajor());
        application.setApplySchool(applicationDto.getApplySchool());
        application.setType(applicationDto.getType());
        application.setApplicationTime(applicationDto.getApplicationTime());
        application.setStatus(applicationDto.getStatus());
        application.setPreInscription(applicationDto.getPreInscription());
        application.setAdmissionTime(applicationDto.getAdmissionTime());
        application.setNotification(applicationDto.getNotification());
        application.setTwoZeroTwo(applicationDto.getTwoZeroTwo());
        application.setAdviser(applicationDto.getAdviser());
        application.setRemark(applicationDto.getRemark());
        application.setFlag(applicationDto.getFlag());
        return application;
    }
}
